package com.threefood.dao;

import java.util.List;

import com.threefood.dao.mapper.UserRowMapper;
import com.threefood.model.User;

public class UserDao extends BaseDao {
    private static final String SELECT_BY_ID = "SELECT * FROM USER WHERE id = ?";
    private static final String LOGIN = "SELECT * FROM USER WHERE name = ? AND password = ?";

    // 根据ID查询
    public User selectUserById(User u) {
        User user = null;
        List<User> users =
            (List<User>) jdbcTemplate.query(SELECT_BY_ID, new Object[] { u.getId() }, new UserRowMapper());
        if (!users.isEmpty()) {
            user = users.get(0);
        }
        return user;
    }

    public User login(User u) {
        User user = null;

        List<User> users =
            (List<User>) jdbcTemplate.query(LOGIN, new Object[] { u.getName(), u.getPassword() },
                    new UserRowMapper());
        if (!users.isEmpty()) {
            user = users.get(0);
        }
        return user;
    }
}
